<?php

declare(strict_types=1);

namespace app\controller\admin;

use think\Response;
use \think\facade\Db;

// 1 未上报
// 2 已上报
// 3 审核通过
// 4 审核不通过

class Faker extends Base
{
    public function audit(): Response
    {
        $ids = $this->request->post("ids");
        $in = implode(",", $ids);
        $r = Db::name("admin_budget_report")->where([
            ['uniq_code', 'in', $in]
        ])->update([
            'status' => 3
        ]);

        return $this->buildSuccess([
            "row" => $r,
        ]);
    }
    public function cancel(): Response
    {
        $ids = $this->request->post("ids");
        $in = implode(",", $ids);
        $r = Db::name("admin_budget_report")->where([
            ['uniq_code', 'in', $in]
        ])->update([
            'status' => 1
        ]);

        return $this->buildSuccess([
            "row" => $r,
        ]);
    }
    public function test(): Response
    {
        $no = Db::query("select count(*) as c from admin_budget_report where status = '1'");
        $wait = Db::query("select count(*) as c from admin_budget_report where status = '2'");
        $pass = Db::query("select count(*) as c from admin_budget_report where status = '3'");

        return $this->buildSuccess([
            "no" => $no[0]['c'],
            "pass" => $pass[0]['c'],
            "wait" => $wait[0]['c'],
        ]);
    }

    /**
     * 208 社会保障
     * 210 卫生健康
     * 211 节能环保
     * 213 农林水
     * 220 自然海洋
     * 221 住房保障
     * 224 灾害防治，应急管理
     * 
     * 5 行政事业单位养老
     * -1 行政单位离退
     * -5 机关事业单位基本养老保险缴费支出
     * 
     * 11 行政事业单位医疗
     * -1 行政单位医疗
     * -3 公务员医疗补助
     * 
     * 4 自然生态保护
     * -1 生态保护
     * 
     * 
     * 6 退耕还林
     * -99 其他退耕还林
     * 
     * 2 林业和草原
     * -5 森林资源培育
     * -99 其他林业
     * 
     * 1 自然资源事务
     * -1 行政运行
     * -6 自然资源利用
     * -50 事业运行
     * -99 其他自然
     * 
     * 2 住房改革
     * -1 住房公积金
     * 
     * 1 应急事务管理
     * -4 灾害风险
     * 
     */


    public function index(): Response
    {
        $code = $this->request->get('code', '');
        $dis_code = substr($code, 0, 6);
        $ins_code = substr($code, 6, 6);
        $thisyears = Db::query("select * from admin_budget where type = 1 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);
        $lastyears = Db::query("select * from admin_budget where type = 2 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);
        // var_dump($thisyears);exit;
        $thisyear = [];
        $lastyear = [];
        $calc = [];

        // 第三部分

        // 一般公共预算基本支出
        // 人员经费
        $ryarr = [30101, 30102, 30102, 30107, 30108, 30110, 30111, 30112, 30113, 30199, 30302, 30305, 30309, 30399];
        // 公用经费
        $gyarr = [30201, 30208, 30211, 30231];


        $total = 0;
        $general_public_budget = 0;
        $government_budgets = 0;
        $institution_other_income = 0;
        $gov_capital = 0;
        $utility_income = 0;
        $institution_management_income = 0;
        $special_financial_account = 0;
        $jbzc = 0; // 基本支出
        $xmzc = 0; // 项目支出
        $fiscal_appropriation = 0;
        $shbzjyzc = 0;
        $wsjkzc = 0;
        $jnhbzc = 0;
        $cxsqzc = 0;
        $nlszc = 0;
        $zrzyhyqxzc = 0;
        $zfbzzc = 0;
        $zhfzzc = 0;
        $ybggfwzc = 0;
        $qtzc = 0;
        $a1 = 0;
        $a2 = 0;
        $a3 = 0;
        $a4 = 0;
        $a5 = 0;
        $a6 = 0;
        $a7 = 0;
        $a8 = 0;
        $a9 = 0;
        $a10 = 0;
        $a11 = 0;
        $a12 = 0;
        $a13 = 0;
        $a14 = 0;
        $a15 = 0;
        $a16 = 0;
        $ygcg = 0;
        $gwycgz = 0;
        $gwycwh = 0;
        $gwjdf = 0;
        $a17 = 0;
        $jgyxjf = 0;
        $project_count = 0;
        $project_payment = 0;

        $in_project = [];
        $ryinclude = [];
        $gyinclude = [];

        $ybggfwzc_arr = [];

        $jbzc1 = 0;
        $xmzc1 = 0;

        foreach ($thisyears as $item) {
            // if ($item['type'] == 2) continue;
            $total += $item['total'];
            $general_public_budget += $item['general_public_budget'];
            $government_budgets += $item['government_budgets'];
            $gov_capital += $item['government_capital_management_budget'];
            $institution_other_income += $item['institution_other_income'];
            $fiscal_appropriation += $item['fiscal_appropriation'];
            $utility_income += $item['utility_income'];
            $institution_management_income += $item['institution_management_income'];
            $special_financial_account += $item['special_financial_account'];

            $jbzc += $item['project_category_name'] == "基本支出" ? $item['total'] : 0;
            $xmzc += $item['project_category_name'] == "项目支出" ? $item['total'] : 0;
            $jbzc1 += $item['project_category_name'] == "基本支出" ? $item['general_public_budget'] : 0;
            $xmzc1 += $item['project_category_name'] == "项目支出" ? $item['general_public_budget'] : 0;

            $shbzjyzc += $item['functional_category_name_class'] == "社会保障和就业支出" ? $item['total'] : 0;
            $wsjkzc += $item['functional_category_name_class'] == "卫生健康支出" ? $item['total'] : 0;
            $jnhbzc += $item['functional_category_name_class'] == "节能环保支出" ? $item['total'] : 0;
            $cxsqzc += $item['functional_category_name_class'] == "城乡社区支出" ? $item['total'] : 0;
            $nlszc += $item['functional_category_name_class'] == "农林水支出" ? $item['total'] : 0;
            $zrzyhyqxzc += $item['functional_category_name_class'] == "自然资源海洋气象等支出" ? $item['total'] : 0;
            $zfbzzc += $item['functional_category_name_class'] == "住房保障支出" ? $item['total'] : 0;
            $zhfzzc += $item['functional_category_name_class'] == "灾害防治及应急管理支出" ? $item['total'] : 0;
            $ybggfwzc += $item['functional_category_name_class'] == "一般公共服务支出" ? $item['total'] : 0;
            $qtzc += $item['functional_category_name_class'] == "其他支出" ? $item['total'] : 0;

            // 一般公共服务支出项
            if ($item['functional_category_code_class'] == 213) {
                $_key = 'k' . $item['functional_category_name_class'] . '_' . $item['functional_category_name_fund'] . '_' . $item['functional_category_name_item'];
                $ybggfwzc_arr[$_key] = 1;
            }

            if ($item['functional_category_code_class'] == 208 && $item['functional_category_code_fund'] == 5) {
                if ($item['functional_category_code_item'] == 1) {
                    $a1 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 5) {
                    $a2 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 210 && $item['functional_category_code_fund'] == 11) {
                if ($item['functional_category_code_item'] == 1) {
                    $a3 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 3) {
                    $a4 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 4) {
                if ($item['functional_category_code_item'] == 1) {
                    $a5 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 6) {
                if ($item['functional_category_code_item'] == 99) {
                    $a6 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 213 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 5) {
                    $a7 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a8 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 220 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 1) {
                    $a9 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 6) {
                    $a10 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 50) {
                    $a11 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a12 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 221 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 1) {
                    $a13 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 224 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 4) {
                    $a14 += $item['general_public_budget'];
                }
            }
            if (in_array($item['economics_category_code'], $ryarr)) {
                $a15 += $item['general_public_budget'];
                if (!in_array($item['economics_category_name'], $ryinclude) && $item['general_public_budget'] > 0) {
                    array_push($ryinclude, $item['economics_category_name']);
                }
            }
            if (in_array($item['economics_category_code'], $gyarr)) {
                $a16 += $item['general_public_budget'];
                if (!in_array($item['economics_category_name'], $gyinclude) && $item['general_public_budget'] > 0) {
                    array_push($gyinclude, $item['economics_category_name']);
                }
            }
            if ($item['economics_category_code'] == "30212") {
                $ygcg += $item['general_public_budget'];
            }
            if (in_array($item['economics_category_code'], [30913, 31013])) {
                $gwycgz += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30231") {
                $gwycwh += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30217") {
                $gwjdf += $item['general_public_budget'];
            }
            if ($item['functional_category_code_class'] == 212 && $item['functional_category_code_fund'] == 8) {
                if ($item['functional_category_code_item'] == 16) {
                    $a17 += $item['general_public_budget'];
                }
            }
            // 公式： 项目类别 - 基本支出  && 经济分类编码 302
            if ($item['economics_category_code_class'] == "302" && $item['project_category_name'] == "基本支出") {
                $jgyxjf += $item['fiscal_appropriation'];
            }
            if ($item['project_category_name'] == "项目支出" && $item['fiscal_appropriation'] > 0) {
                // 重复的project_code 算一个项目
                if (!in_array($item['project_code'], $in_project)) {
                    $project_count += 1;
                    array_push($in_project, $item['project_code']);
                }

                $project_payment += $item['fiscal_appropriation'];
            }
        }
        $thisyear['total'] = $total;
        $thisyear['general_public_budget'] = $general_public_budget;
        $thisyear['government_budgets'] = $government_budgets;
        $thisyear['institution_other_income'] = $institution_other_income;
        $thisyear['fiscal_appropriation'] = $fiscal_appropriation;
        $thisyear['gov_capital'] = $gov_capital;
        $thisyear['utility_income'] = $utility_income;
        $thisyear['institution_management_income'] = $institution_management_income;
        $thisyear['special_financial_account'] = $special_financial_account;
        $thisyear['jbzc'] = $jbzc;
        $thisyear['xmzc'] = $xmzc;
        $thisyear['jbzc1'] = $jbzc1;
        $thisyear['xmzc1'] = $xmzc1;
        $thisyear['shbzjyzc'] = $shbzjyzc;
        $thisyear['wsjkzc'] = $wsjkzc;
        $thisyear['jnhbzc'] = $jnhbzc;
        $thisyear['cxsqzc'] = $cxsqzc;
        $thisyear['nlszc'] = $nlszc;
        $thisyear['zrzyhyqxzc'] = $zrzyhyqxzc;
        $thisyear['zfbzzc'] = $zfbzzc;
        $thisyear['zhfzzc'] = $zhfzzc;
        $thisyear['ybggfwzc'] = $ybggfwzc;
        $thisyear['qtzc'] = $qtzc;
        $thisyear['a1'] = $a1;
        $thisyear['a2'] = $a2;
        $thisyear['a3'] = $a3;
        $thisyear['a4'] = $a4;
        $thisyear['a5'] = $a5;
        $thisyear['a6'] = $a6;
        $thisyear['a7'] = $a7;
        $thisyear['a8'] = $a8;
        $thisyear['a9'] = $a9;
        $thisyear['a10'] = $a10;
        $thisyear['a11'] = $a11;
        $thisyear['a12'] = $a12;
        $thisyear['a13'] = $a13;
        $thisyear['a14'] = $a14;
        $thisyear['a15'] = $a15;
        $thisyear['a16'] = $a16;
        $thisyear['a17'] = $a17;
        $thisyear['ygcg'] = $ygcg;
        $thisyear['gwycgz'] = $gwycgz;
        $thisyear['gwycwh'] = $gwycwh;
        $thisyear['gwjdf'] = $gwjdf;
        $thisyear['sgtotal'] = $ygcg + $gwycgz + $gwycwh + $gwjdf;
        $thisyear['jgyxjf'] = $jgyxjf;
        $thisyear['project_count'] = $project_count;
        $thisyear['project_payment'] = $project_payment;
        $thisyear['ryinclude'] = implode("、", $ryinclude);
        $thisyear['gyinclude'] = implode("、", $gyinclude);
        $thisyear['ybggfwzc_arr'] = $ybggfwzc_arr;


        $total = 0;
        $general_public_budget = 0;
        $government_budgets = 0;
        $gov_capital = 0;
        $institution_other_income = 0;
        $utility_income = 0;
        $institution_management_income = 0;
        $jbzc = 0;
        $xmzc = 0;
        $fiscal_appropriation = 0;
        $shbzjyzc = 0;
        $wsjkzc = 0;
        $jnhbzc = 0;
        $cxsqzc = 0;
        $nlszc = 0;
        $zrzyhyqxzc = 0;
        $zfbzzc = 0;
        $zhfzzc = 0;
        $ybggfwzc = 0;
        $qtzc = 0;
        $a1 = 0;
        $a2 = 0;
        $a3 = 0;
        $a4 = 0;
        $a5 = 0;
        $a6 = 0;
        $a7 = 0;
        $a8 = 0;
        $a9 = 0;
        $a10 = 0;
        $a11 = 0;
        $a12 = 0;
        $a13 = 0;
        $a14 = 0;
        $a17 = 0;
        $ygcg = 0;
        $gwycgz = 0;
        $gwycwh = 0;
        $gwjdf = 0;
        $jgyxjf = 0;
        $jbzc1 = 0;
        $xmzc1 = 0;

        foreach ($lastyears as $item) {
            // if ($item['type'] == 1) continue;
            $total += $item['total'];
            $general_public_budget += $item['general_public_budget'];
            $government_budgets += $item['government_budgets'];
            $gov_capital += $item['government_capital_management_budget'];
            $institution_other_income += $item['institution_other_income'];
            $fiscal_appropriation += $item['fiscal_appropriation'];
            $utility_income += $item['utility_income'];
            $institution_management_income += $item['institution_management_income'];

            $jbzc += $item['project_category_name'] == "基本支出" ? $item['total'] : 0;
            $xmzc += $item['project_category_name'] == "项目支出" ? $item['total'] : 0;

            $jbzc1 += $item['project_category_name'] == "基本支出" ? $item['general_public_budget'] : 0;
            $xmzc1 += $item['project_category_name'] == "项目支出" ? $item['general_public_budget'] : 0;

            $shbzjyzc += $item['functional_category_name_class'] == "社会保障和就业支出" ? $item['total'] : 0;
            $wsjkzc += $item['functional_category_name_class'] == "卫生健康支出" ? $item['total'] : 0;
            $jnhbzc += $item['functional_category_name_class'] == "节能环保支出" ? $item['total'] : 0;
            $cxsqzc += $item['functional_category_name_class'] == "城乡社区支出" ? $item['total'] : 0;
            $nlszc += $item['functional_category_name_class'] == "农林水支出" ? $item['total'] : 0;
            $zrzyhyqxzc += $item['functional_category_name_class'] == "自然资源海洋气象等支出" ? $item['total'] : 0;
            $zfbzzc += $item['functional_category_name_class'] == "住房保障支出" ? $item['total'] : 0;
            $zhfzzc += $item['functional_category_name_class'] == "灾害防治及应急管理支出" ? $item['total'] : 0;
            $ybggfwzc += $item['functional_category_name_class'] == "一般公共服务支出" ? $item['total'] : 0;
            $qtzc += $item['functional_category_name_class'] == "其他支出" ? $item['total'] : 0;

            if ($item['functional_category_code_class'] == 208 && $item['functional_category_code_fund'] == 5) {
                if ($item['functional_category_code_item'] == 1) {
                    $a1 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 5) {
                    $a2 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 210 && $item['functional_category_code_fund'] == 11) {
                if ($item['functional_category_code_item'] == 1) {
                    $a3 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 3) {
                    $a4 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 4) {
                if ($item['functional_category_code_item'] == 1) {
                    $a5 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 6) {
                if ($item['functional_category_code_item'] == 99) {
                    $a6 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 213 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 5) {
                    $a7 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a8 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 220 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 1) {
                    $a9 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 6) {
                    $a10 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 50) {
                    $a11 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a12 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 221 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 1) {
                    $a13 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 224 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 4) {
                    $a14 += $item['general_public_budget'];
                }
            }
            if ($item['economics_category_code'] == "30212") {
                $ygcg += $item['general_public_budget'];
            }
            if (in_array($item['economics_category_code'], [30913, 31013])) {
                $gwycgz += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30231") {
                $gwycwh += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30217") {
                $gwjdf += $item['general_public_budget'];
            }
            if ($item['functional_category_code_class'] == 212 && $item['functional_category_code_fund'] == 8) {
                if ($item['functional_category_code_item'] == 16) {
                    $a17 += $item['general_public_budget'];
                }
            }
            // if ($item['economics_category_code_class'] == "302") {
            //     $jgyxjf += $item['fiscal_appropriation'];
            // }
            // 公式： 项目类别 - 基本支出  && 经济分类编码 302
            if ($item['economics_category_code_class'] == "302" && $item['project_category_name'] == "基本支出") {
                $jgyxjf += $item['fiscal_appropriation'];
            }
        }
        $lastyear['total'] = $total;
        $lastyear['general_public_budget'] = $general_public_budget;
        $lastyear['government_budgets'] = $government_budgets;
        $lastyear['gov_capital'] = $gov_capital;
        $lastyear['institution_other_income'] = $institution_other_income;
        $lastyear['fiscal_appropriation'] = $fiscal_appropriation;
        $lastyear['utility_income'] = $utility_income;
        $lastyear['institution_management_income'] = $institution_management_income;
        $lastyear['jbzc'] = $jbzc;
        $lastyear['xmzc'] = $xmzc;
        $lastyear['jbzc1'] = $jbzc1;
        $lastyear['xmzc1'] = $xmzc1;
        $lastyear['shbzjyzc'] = $shbzjyzc;
        $lastyear['wsjkzc'] = $wsjkzc;
        $lastyear['jnhbzc'] = $jnhbzc;
        $lastyear['cxsqzc'] = $cxsqzc;
        $lastyear['nlszc'] = $nlszc;
        $lastyear['zrzyhyqxzc'] = $zrzyhyqxzc;
        $lastyear['zfbzzc'] = $zfbzzc;
        $lastyear['zhfzzc'] = $zhfzzc;
        $lastyear['ybggfwzc'] = $ybggfwzc;
        $lastyear['qtzc'] = $qtzc;
        $lastyear['a1'] = $a1;
        $lastyear['a2'] = $a2;
        $lastyear['a3'] = $a3;
        $lastyear['a4'] = $a4;
        $lastyear['a5'] = $a5;
        $lastyear['a6'] = $a6;
        $lastyear['a7'] = $a7;
        $lastyear['a8'] = $a8;
        $lastyear['a9'] = $a9;
        $lastyear['a10'] = $a10;
        $lastyear['a11'] = $a11;
        $lastyear['a12'] = $a12;
        $lastyear['a13'] = $a13;
        $lastyear['a14'] = $a14;
        $lastyear['a17'] = $a17;
        $lastyear['ygcg'] = $ygcg;
        $lastyear['gwycgz'] = $gwycgz;
        $lastyear['gwycwh'] = $gwycwh;
        $lastyear['gwjdf'] = $gwjdf;
        $lastyear['sgtotal'] = $ygcg + $gwycgz + $gwycwh + $gwjdf;
        $lastyear['jgyxjf'] = $jgyxjf;

        // 标题二
        $calc['v1'] = $thisyear['total'] == 0 ? 100 : ($thisyear['general_public_budget'] / $thisyear['total']) * 100;
        $calc['v2'] = $thisyear['general_public_budget'] - $lastyear['general_public_budget'];
        // $calc['v3'] = $thisyear['general_public_budget'] == 0 ? 0 : 100 * (($thisyear['general_public_budget'] - $lastyear['general_public_budget']) / $thisyear['general_public_budget']);
        $calc['v3'] = $lastyear['general_public_budget'] == 0 ? 100 : (($thisyear['general_public_budget'] - $lastyear['general_public_budget']) / $lastyear['general_public_budget']) * 100;

        $calc['v4'] = $thisyear['total'] == 0 ? 100 : ($thisyear['government_budgets'] / $thisyear['total']) * 100;
        $calc['v5'] = $thisyear['government_budgets'] - $lastyear['government_budgets'];
        // $calc['v6'] = $thisyear['government_budgets'] == 0 ? 0 : 100 * (($thisyear['government_budgets'] - $lastyear['government_budgets']) / $thisyear['government_budgets']);
        $calc['v6'] = $lastyear['government_budgets'] == 0 ? 100 : (($thisyear['government_budgets'] - $lastyear['government_budgets']) / $lastyear['government_budgets']) * 100;

        $calc['v7'] = $thisyear['total'] == 0 ? 100 : ($thisyear['institution_other_income'] / $thisyear['total']) * 100;
        $calc['v8'] = $thisyear['institution_other_income'] - $lastyear['institution_other_income'];
        // $calc['v9'] = $thisyear['institution_other_income'] == 0 ? 0 : 100 * (($thisyear['institution_other_income'] - $lastyear['institution_other_income']) / $thisyear['institution_other_income']);
        $calc['v9'] = $lastyear['institution_other_income'] == 0 ? 100 : (($thisyear['institution_other_income'] - $lastyear['institution_other_income']) / $lastyear['institution_other_income']) * 100;
        // 标题三
        $calc['v10'] = $thisyear['total'] == 0 ? 100 : ($thisyear['jbzc'] / $thisyear['total']) * 100;
        $calc['v11'] = $thisyear['jbzc'] - $lastyear['jbzc'];
        $calc['c11'] = $this->calc1($thisyear['jbzc1'], $lastyear['jbzc1']);
        // $calc['v12'] = $thisyear['jbzc'] == 0 ? 0 : 100 * (($thisyear['jbzc'] - $lastyear['jbzc']) / $thisyear['jbzc']);
        // $calc['v12'] = $lastyear['jbzc'] == 0 ? 100 : (($thisyear['jbzc'] - $lastyear['jbzc']) / $lastyear['jbzc']) * 100;
        // $calc['v12'] =  $thisyear['xmzc'] == 0 ? 100 : 100 * ($thisyear['jbzc'] / ($thisyear['jbzc'] + $thisyear['xmzc']));
        $calc['v12'] =  $lastyear['jbzc'] == 0 ? 100 : 100 * (($thisyear['jbzc'] - $lastyear['jbzc']) / $lastyear['jbzc']);
        $calc['c12'] =  $this->calc2($thisyear['jbzc1'], $lastyear['jbzc1']);

        $calc['v13'] = $thisyear['total'] == 0 ? 100 : ($thisyear['xmzc'] / $thisyear['total']) * 100;
        $calc['v14'] = $thisyear['xmzc'] - $lastyear['xmzc'];
        $calc['c14'] = $this->calc1($thisyear['xmzc1'], $lastyear['xmzc1']);
        // $calc['v15'] = $thisyear['xmzc'] == 0 ? 0 : 100 * (($thisyear['xmzc'] - $lastyear['xmzc']) / $thisyear['xmzc']);
        // $calc['v15'] = $lastyear['xmzc'] == 0 ? 100 : (($thisyear['xmzc'] - $lastyear['xmzc']) / $lastyear['xmzc']) * 100;
        // var_dump($thisyear['jbzc'], $thisyear['xmzc']);
        // $calc['v15'] = 100 * ($thisyear['xmzc'] / ($thisyear['jbzc'] + $thisyear['xmzc']));
        $calc['v15'] =  $lastyear['xmzc'] == 0 ? 100 : 100 * (($thisyear['xmzc'] - $lastyear['xmzc']) / $lastyear['xmzc']);
        $calc['c15'] =  $this->calc2($thisyear['xmzc1'], $lastyear['xmzc1']);

        // 标题四

        // 标题五
        // $calc['v15'] = $thisyear['jbzc'] - $lastyear['jbzc'];
        $calc['v16'] = $thisyear['jbzc'] == 0 ? 100 : 100 * (($thisyear['jbzc'] - $lastyear['jbzc']) / $thisyear['jbzc']);
        $calc['v17'] = $thisyear['xmzc'] - $lastyear['xmzc'];
        $calc['v18'] = $thisyear['xmzc'] == 0 ? 100 : 100 * (($thisyear['xmzc'] - $lastyear['xmzc']) / $thisyear['xmzc']);

        $calc['v19'] = $thisyear['total'] == 0 ? 100 : ($thisyear['shbzjyzc'] / $thisyear['total']) * 100;
        $calc['v20'] = $thisyear['total'] == 0 ? 100 : ($thisyear['wsjkzc'] / $thisyear['total']) * 100;
        $calc['v21'] = $thisyear['total'] == 0 ? 100 : ($thisyear['jnhbzc'] / $thisyear['total']) * 100;
        $calc['v22'] = $thisyear['total'] == 0 ? 100 : ($thisyear['nlszc'] / $thisyear['total']) * 100;
        $calc['v23'] = $thisyear['total'] == 0 ? 100 : ($thisyear['zrzyhyqxzc'] / $thisyear['total']) * 100;
        $calc['v24'] = $thisyear['total'] == 0 ? 100 : ($thisyear['zfbzzc'] / $thisyear['total']) * 100;
        $calc['v25'] = $thisyear['total'] == 0 ? 100 : ($thisyear['zhfzzc'] / $thisyear['total']) * 100;
        $calc['f4'] = $thisyear['total'] == 0 ? 100 : ($thisyear['ybggfwzc'] / $thisyear['total']) * 100;

        $calc['v26'] = $thisyear['a1'] - $lastyear['a1'];
        // $calc['v27'] = $thisyear['a1'] == 0 ? 0 : (($thisyear['a1'] - $lastyear['a1']) / $thisyear['a1']) * 100;
        $calc['v27'] = $lastyear['a1'] == 0 ? 100 : 100 * (($thisyear['a1'] - $lastyear['a1']) / $lastyear['a1']);

        $calc['v28'] = $thisyear['a2'] - $lastyear['a2'];
        // $calc['v29'] = $thisyear['a2'] == 0 ? 0 : (($thisyear['a2'] - $lastyear['a2']) / $thisyear['a2']) * 100;
        $calc['v29'] = $lastyear['a2'] == 0 ? 100 : 100 * (($thisyear['a2'] - $lastyear['a2']) / $lastyear['a2']);

        $calc['v30'] = $thisyear['a3'] - $lastyear['a3'];
        // $calc['v31'] = $thisyear['a3'] == 0 ? 0 : (($thisyear['a3'] - $lastyear['a3']) / $thisyear['a3']) * 100;
        $calc['v31'] = $lastyear['a3'] == 0 ? 100 : 100 * (($thisyear['a3'] - $lastyear['a3']) / $lastyear['a3']);

        $calc['v32'] = $thisyear['a4'] - $lastyear['a4'];
        // $calc['v33'] = $thisyear['a4'] == 0 ? 0 : (($thisyear['a4'] - $lastyear['a4']) / $thisyear['a4']) * 100;
        $calc['v33'] = $lastyear['a4'] == 0 ? 100 : 100 * (($thisyear['a4'] - $lastyear['a4']) / $lastyear['a4']);

        $calc['v34'] = $thisyear['a5'] - $lastyear['a5'];
        // $calc['v35'] = $thisyear['a5'] == 0 ? 0 : (($thisyear['a5'] - $lastyear['a5']) / $thisyear['a5']) * 100;
        $calc['v35'] = $lastyear['a5'] == 0 ? 100 : 100 * (($thisyear['a5'] - $lastyear['a5']) / $lastyear['a5']);

        $calc['v36'] = $thisyear['a6'] - $lastyear['a6'];
        // $calc['v37'] = $thisyear['a6'] == 0 ? 0 : (($thisyear['a6'] - $lastyear['a6']) / $thisyear['a6']) * 100;
        $calc['v37'] = $lastyear['a6'] == 0 ? 100 : 100 * (($thisyear['a6'] - $lastyear['a6']) / $lastyear['a6']);

        $calc['v38'] = $thisyear['a7'] - $lastyear['a7'];
        // $calc['v39'] = $thisyear['a7'] == 0 ? 0 : (($thisyear['a7'] - $lastyear['a7']) / $thisyear['a7']) * 100;
        $calc['v39'] = $lastyear['a7'] == 0 ? 100 : 100 * (($thisyear['a7'] - $lastyear['a7']) / $lastyear['a7']);

        $calc['v40'] = $thisyear['a8'] - $lastyear['a8'];
        // $calc['v41'] = $thisyear['a8'] == 0 ? 0 : (($thisyear['a8'] - $lastyear['a8']) / $thisyear['a8']) * 100;
        $calc['v41'] = $lastyear['a8'] == 0 ? 100 : 100 * (($thisyear['a8'] - $lastyear['a8']) / $lastyear['a8']);

        $calc['v42'] = $thisyear['a9'] - $lastyear['a9'];
        // $calc['v43'] = $thisyear['a9'] == 0 ? 0 : (($thisyear['a9'] - $lastyear['a9']) / $thisyear['a9']) * 100;
        $calc['v43'] = $lastyear['a9'] == 0 ? 100 : 100 * (($thisyear['a9'] - $lastyear['a9']) / $lastyear['a9']);

        $calc['v44'] = $thisyear['a10'] - $lastyear['a10'];
        // $calc['v45'] = $thisyear['a10'] == 0 ? 0 : (($thisyear['a10'] - $lastyear['a10']) / $thisyear['a10']) * 100;
        $calc['v45'] = $lastyear['a10'] == 0 ? 100 : 100 * (($thisyear['a10'] - $lastyear['a10']) / $lastyear['a10']);

        $calc['v46'] = $thisyear['a11'] - $lastyear['a11'];
        // $calc['v47'] = $thisyear['a11'] == 0 ? 0 : (($thisyear['a11'] - $lastyear['a11']) / $thisyear['a11']) * 100;
        $calc['v47'] = $lastyear['a11'] == 0 ? 100 : 100 * (($thisyear['a11'] - $lastyear['a11']) / $lastyear['a11']);

        $calc['v48'] = $thisyear['a12'] - $lastyear['a12'];
        // $calc['v49'] = $thisyear['a12'] == 0 ? 0 : (($thisyear['a12'] - $lastyear['a12']) / $thisyear['a12']) * 100;
        $calc['v49'] = $lastyear['a12'] == 0 ? 100 : 100 * (($thisyear['a12'] - $lastyear['a12']) / $lastyear['a12']);

        $calc['v50'] = $thisyear['a13'] - $lastyear['a13'];
        // $calc['v51'] = $thisyear['a13'] == 0 ? 0 : (($thisyear['a13'] - $lastyear['a13']) / $thisyear['a13']) * 100;
        $calc['v51'] = $lastyear['a13'] == 0 ? 100 : 100 * (($thisyear['a13'] - $lastyear['a13']) / $lastyear['a13']);

        $calc['v52'] = $thisyear['a14'] - $lastyear['a14'];
        // $calc['v53'] = $thisyear['a14'] == 0 ? 0 : (($thisyear['a14'] - $lastyear['a14']) / $thisyear['a14']) * 100;
        $calc['v53'] = $lastyear['a14'] == 0 ? 100 : 100 * (($thisyear['a14'] - $lastyear['a14']) / $lastyear['a14']);

        $calc['v54'] = $thisyear['sgtotal'] - $lastyear['sgtotal'];
        // $calc['v55'] = $thisyear['sgtotal'] == 0 ? 0 : (($thisyear['sgtotal'] - $lastyear['sgtotal']) / $thisyear['sgtotal']) * 100;
        $calc['v55'] = $lastyear['sgtotal'] == 0 ? 100 : (($thisyear['sgtotal'] - $lastyear['sgtotal']) / $lastyear['sgtotal']) * 100;
        $calc['c55'] = $this->calc2($thisyear['sgtotal'], $lastyear['sgtotal']);

        $calc['v56'] = $thisyear['ygcg'] - $lastyear['ygcg'];
        $calc['v57'] = $lastyear['ygcg'] == 0 ? 100 : (($thisyear['ygcg'] - $lastyear['ygcg']) / $lastyear['ygcg']) * 100;
        $calc['c57'] = $this->calc2($thisyear['ygcg'], $lastyear['ygcg']);

        $calc['v58'] = $thisyear['a17'] - $lastyear['a17'];
        $calc['v59'] = $thisyear['a17'] == 0 ? 0 : (($thisyear['a17'] - $lastyear['a17']) / $thisyear['a17']) * 100;

        $calc['v60'] = $thisyear['jgyxjf'] - $lastyear['jgyxjf'];
        $calc['f1'] = $thisyear['gwycgz'] - $lastyear['gwycgz'];
        $calc['f2'] = $thisyear['gwycwh'] - $lastyear['gwycwh'];
        $calc['f3'] = $thisyear['gwjdf'] - $lastyear['gwjdf'];
        $calc['cf1'] = $this->calc2($thisyear['gwycgz'], $lastyear['gwycgz']);
        $calc['cf2'] = $this->calc2($thisyear['gwycwh'], $lastyear['gwycwh']);
        $calc['cf3'] = $this->calc2($thisyear['gwjdf'], $lastyear['gwjdf']);
        // $calc['v61'] = $thisyear['jgyxjf'] == 0 ? 0 : (($thisyear['jgyxjf'] - $lastyear['jgyxjf']) / $thisyear['jgyxjf']) * 100;
        $calc['v61'] = $lastyear['jgyxjf'] == 0 ? 100 : 100 * (($thisyear['jgyxjf'] - $lastyear['jgyxjf']) / $lastyear['jgyxjf']);

        $calc['v62'] = $lastyear['gwycgz'] == 0 ? 100 : (($thisyear['gwycgz'] - $lastyear['gwycgz']) / $lastyear['gwycgz']) * 100;
        $calc['v63'] = $lastyear['gwjdf'] == 0 ? 100 : (($thisyear['gwjdf'] - $lastyear['gwjdf']) / $lastyear['gwjdf']) * 100;
        $calc['v64'] = $lastyear['gwycwh'] == 0 ? 100 : (($thisyear['gwycwh'] - $lastyear['gwycwh']) / $lastyear['gwycwh']) * 100;

        $calc['v70'] = $thisyear['total'] == 0 ? 100 : ($thisyear['gov_capital'] / $thisyear['total']) * 100;
        $calc['v71'] = $thisyear['gov_capital'] - $lastyear['gov_capital'];
        // $calc['v72'] = $thisyear['gov_capital'] == 0 ? 0 : 100 * (($thisyear['gov_capital'] - $lastyear['gov_capital']) / $thisyear['gov_capital']);
        $calc['v72'] = $lastyear['gov_capital'] == 0 ? 100 : (($thisyear['gov_capital'] - $lastyear['gov_capital']) / $lastyear['gov_capital']) * 100;

        // 事业单位
        $calc['v81'] = $this->calc1($thisyear['utility_income'], $lastyear['utility_income']);
        $calc['v82'] = $this->calc2($thisyear['utility_income'], $lastyear['utility_income']);
        $calc['v83'] = $this->calc3($thisyear['utility_income'], $thisyear['total']);

        $other = [];
        $other2 = [];
        foreach ($thisyear as $k => $v) {
            if (!in_array($k, ['project_count', 'ryinclude', 'gyinclude', 'ybggfwzc_arr'])) {
                $thisyear[$k] = $v / 10000;
            }
            if (in_array($k, ['shbzjyzc', 'wsjkzc', 'jnhbzc', 'cxsqzc', 'nlszc', 'zrzyhyqxzc', 'zfbzzc', 'zhfzzc', 'ybggfwzc']) && $v != 0) {
                array_push($other, $k);
            }
            if (in_array($k, ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'a11', 'a12', 'a13', 'a14']) && $v != 0) {
                array_push($other2, $k);
            }
        }
        foreach ($lastyear as $k => $v) {
            $lastyear[$k] = number_format($v / 10000, 2, ".", "");
        }

        foreach ($calc as $k => $v) {
            if (in_array($k, [
                'v2',
                'v5',
                'v8',
                'v11',
                'v14',
                'v17',
                'v26',
                'v28',
                'v30',
                'v32',
                'v34',
                'v36',
                'v38',
                'v40',
                'v42',
                'v44',
                'v46',
                'v48',
                'v50',
                'v52',
                'v54',
                'v56',
                'v58',
                'v60',
                'f1',
                'f2',
                'f3',
                'v71',
            ])) {
                $calc[$k] = $v / 10000;
            }
        }

        $include4 = "";

        if ($thisyear['general_public_budget'] > 0) {
            $include4 .= "一般公共预算拨款" . $this->formatunit($thisyear['general_public_budget']) . "万元、";
        }
        if ($thisyear['government_budgets'] > 0) {
            $include4 .= "政府性基金预算拨款" . $this->formatunit($thisyear['government_budgets']) . "万元、";
        }
        if ($thisyear['gov_capital'] > 0) {
            $include4 .= "国有资本经营预算" . $this->formatunit($thisyear['gov_capital']) . "万元、";
        }
        if ($thisyear['special_financial_account'] > 0) {
            $include4 .= "财政专户" . $this->formatunit($thisyear['special_financial_account']) . "万元、";
        }
        if ($thisyear['utility_income'] > 0) {
            $include4 .= "事业收入" . $this->formatunit($thisyear['utility_income']) . "万元、";
        }
        if ($thisyear['institution_management_income'] > 0) {
            $include4 .= "事业单位经营收入" . $this->formatunit($thisyear['institution_management_income']) . "万元、";
        }
        if ($thisyear['institution_other_income'] > 0) {
            $include4 .= "单位其他收入" . $this->formatunit($thisyear['institution_other_income']) . "万元、";
        }
        $thisyear['include4'] = trim($include4, "、") . "。";
        return $this->buildSuccess([
            "thisyear" => $thisyear,
            "lastyear" => $lastyear,
            "calc" => $calc,
            "year" => date("Y"),
            "month" => date("m"),
            "day" => date("d"),
            "others" => $other,
            "others2" => $other2,
            "others3" => [
                'general_public_budget' => [
                    'title' => '一般公共',
                    'total' => $thisyear['general_public_budget']
                ],
                'government_budgets' => [
                    'title' => '政府性基金',
                    'total' => $thisyear['government_budgets']
                ],
                'gov_capital' => [
                    'title' => '国有资本',
                    'total' => $thisyear['gov_capital']
                ],
                // 'fiscal_appropriation' => [
                //     'title' => '财政拨款',
                //     'total' => $thisyear['fiscal_appropriation']
                // ],
                'utility_income' => [
                    'title' => '事业收入',
                    'total' => $thisyear['utility_income']
                ],
                'institution_management_income' => [
                    'title' => '事业单位经营收入',
                    'total' => $thisyear['institution_management_income']
                ],
                'institution_other_income' => [
                    'title' => '单位其他资金收入',
                    'total' => $thisyear['institution_other_income']
                ],
            ]
        ]);
    }

    public function pub()
    {
        $code = $this->request->get('code', '');
        $dis_code = substr($code, 0, 6);
        $ins_code = substr($code, 6, 6);
        $thisyears = Db::query("select * from admin_budget where type = 1 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);
        $lastyears = Db::query("select * from admin_budget where type = 2 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);

        // 一般公共预算 今年
        $arr = [];
        // 政府性基金
        $zfxjj = [];
        // 人员经费
        $ryzc = 0;
        $ryarr = [];
        $ryarrsort = [];
        // 公用经费
        $gyzc = 0;
        $gyarr = [];
        $gyarrsort = [];
        // 支出预算 - 按一般公共预算汇总
        $zcbudget = [];
        // 支出预算 - 按total汇总
        $zcbudget2 = [];
        // 总计
        $total = 0;
        $table3 = [];
        $table9 = [];
        $table7 = [];
        $zf_jbzc = 0;
        $zf_xmzc = 0;

        // table 7 总计
        $total7 = 0;
        $table71 = 0;
        $table72 = 0;
        $table73 = 0;
        $table74 = 0;
        $table75 = 0;
        $table76 = 0;
        $table77 = 0;
        $table78 = 0;
        $table79 = 0;
        $table710 = 0;
        // table 4
        $table4 = $this->initTable4();
        $general_public_budget = 0;
        $government_budgets = 0;
        $gov_capital = 0;
        // $institution_other_income = 0;
        $fiscal_appropriation = 0;
        // 项目列表
        $project_list = [];

        foreach ($thisyears as $row) {
            if ($row['project_code'] && $row['project_category_name'] == '项目支出' && $row['general_public_budget'] > 0) {
                if (!in_array($row['project_code'], ['00010001', '00010002'])) {
                    if (isset($project_list[$row['project_code']])) {
                        $project_list[$row['project_code']]['budget'] += $row['general_public_budget'];
                    } else {
                        $project_list[$row['project_code']] = [
                            'project_name' => $row['project_name'],
                            'budget' => $row['general_public_budget'],
                            // 'info' => $row['project_info'],
                            'vmodel1' => 'vm1' . $row['project_code'],
                            'vmodel2' => 'vm2' . $row['project_code'],
                            'vmodel3' => 'vm3' . $row['project_code'],
                            'vmodel4' => 'vm4' . $row['project_code'],
                            'vmodel5' => 'vm5' . $row['project_code'],
                            'vmodel6' => 'vm6' . $row['project_code'],
                            'vmodel7' => 'vm7' . $row['project_code'],
                            'vmodel8' => 'vm8' . $row['project_code'],
                            'vmodel9' => 'vm9' . $row['project_code'],
                            'vmodel0' => 'vm10' . $row['project_code'],
                            'vmodel11' => 'vm11' . $row['project_code'],
                            'vmodel12' => 'vm12' . $row['project_code'],
                            'vmodel13' => 'vm13' . $row['project_code'],
                        ];
                    }
                }
            }
            $general_public_budget += $row['general_public_budget'];
            $government_budgets += $row['government_budgets'];
            $gov_capital += $row['government_capital_management_budget'];
            // $institution_other_income += $row['institution_other_income'];
            $fiscal_appropriation += $row['fiscal_appropriation'];

            $total += $row['general_public_budget'];
            $key = $row['functional_category_code_class'] . $row['functional_category_code_fund'] . $row['functional_category_code_item'];
            $title = $row['functional_category_name_class'] . '（类）' . $row['functional_category_name_fund'] . '（款）' . $row['functional_category_name_item'] . '（项）';

            // 一般公共预算
            if ($row['general_public_budget'] > 0) {
                if (isset($arr[$key])) {
                    $arr[$key]['total'] += $row['general_public_budget'];
                    $arr[$key]['c1'] += $row['general_public_budget'];
                } else {
                    $arr[$key] = [
                        'title' => $title,
                        'total' => $row['general_public_budget'],
                        'c1' => $row['general_public_budget'],
                        'c2' => 0,
                        'vmodel' => 'reason' . $key,
                        'c5' => 100
                    ];
                }
                if ($row['project_category_name'] == '基本支出') {
                    // 人员支出
                    if (in_array($row['economics_category_code_class'], ['301', '303'])) {
                        $ryzc += $row['general_public_budget'];
                        if (!in_array($row['economics_category_name'], $ryarr)) {
                            array_push($ryarr, $row['economics_category_name']);
                            $ryarrsort[$row['economics_category_code']] = $row['economics_category_name'];
                        }
                    }
                    // 公用支出
                    if ($row['economics_category_code_class'] == '302') {
                        $gyzc += $row['general_public_budget'];
                        if (!in_array($row['economics_category_name'], $gyarr)) {
                            array_push($gyarr, $row['economics_category_name']);
                            $gyarrsort[$row['economics_category_code']] = $row['economics_category_name'];
                        }
                    }
                }
                if ($row['project_category_name'] == '项目支出') {
                    $total7 += $row['general_public_budget'];
                    $table71 += $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0;
                    $table72 += $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0;
                    $table73 += $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0;
                    $table74 += $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0;
                    $table75 += $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0;
                    $table76 += $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0;
                    $table77 += $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0;
                    $table78 += $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0;
                    $table79 += $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0;
                    $table710 += $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0;

                    // table 7
                    // level 1
                    $key7_1 = $row['functional_category_code_class'];
                    $key7_2 = $key7_1 . $row['functional_category_code_fund'];
                    $key7_3 = $key7_2 . $row['functional_category_code_item'];
                    // table 7 - level 1
                    if (isset($table7[$key7_1 . '0000'])) {
                        $table7[$key7_1 . '0000']['total'] += $row['general_public_budget'];
                        $table7[$key7_1 . '0000']['fuli'] += $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['shangpin'] += $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['geren'] += $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['zhaiwu'] += $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['zibenjianshe'] += $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['ziben'] += $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['qiyejianshe'] += $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['qiye'] += $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['baozhang'] += $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_1 . '0000']['qita'] += $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0;
                        // $table7[$key7_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                        // $table7[$key7_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
                    } else {
                        $table7[$key7_1 . '0000'] = [
                            'class' => $row['functional_category_code_class'],
                            'fund' => '', //$row['functional_category_code_fund'],
                            'item' => '', //$row['functional_category_code_item'],
                            'total' => $row['general_public_budget'],
                            // 'jbzc' => $row['project_category_name'] == "基本支出" ? $row['general_public_budget'] : 0,
                            // 'xmzc' => $row['project_category_name'] == "项目支出" ? $row['general_public_budget'] : 0,
                            'name' => $row['functional_category_name_class'],
                            'projectname' => '', //$row['project_name'],
                            'fuli' => $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0,
                            'shangpin' => $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0,
                            'geren' => $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0,
                            'zhaiwu' => $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0,
                            'zibenjianshe' => $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0,
                            'ziben' => $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0,
                            'qiyejianshe' => $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0,
                            'qiye' => $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0,
                            'baozhang' => $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0,
                            'qita' => $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0,
                        ];
                    }
                    // table 7 - level 2
                    if (isset($table7[$key7_2 . '00'])) {
                        $table7[$key7_2 . '00']['total'] += $row['general_public_budget'];
                        $table7[$key7_2 . '00']['fuli'] += $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['shangpin'] += $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['geren'] += $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['zhaiwu'] += $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['zibenjianshe'] += $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['ziben'] += $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['qiyejianshe'] += $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['qiye'] += $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['baozhang'] += $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_2 . '00']['qita'] += $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0;
                        // $table7[$key7_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                        // $table7[$key7_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
                    } else {
                        $table7[$key7_2 . '00'] = [
                            'class' => $row['functional_category_code_class'],
                            'fund' => $row['functional_category_code_fund'],
                            'item' => '', //$row['functional_category_code_item'],
                            'total' => $row['general_public_budget'],
                            // 'jbzc' => $row['project_category_name'] == "基本支出" ? $row['general_public_budget'] : 0,
                            // 'xmzc' => $row['project_category_name'] == "项目支出" ? $row['general_public_budget'] : 0,
                            'name' => $row['functional_category_name_fund'],
                            'projectname' => '', //$row['project_name'],
                            'fuli' => $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0,
                            'shangpin' => $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0,
                            'geren' => $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0,
                            'zhaiwu' => $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0,
                            'zibenjianshe' => $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0,
                            'ziben' => $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0,
                            'qiyejianshe' => $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0,
                            'qiye' => $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0,
                            'baozhang' => $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0,
                            'qita' => $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0,
                        ];
                    }
                    // table 7 - level 3
                    if (isset($table7[$key7_3])) {
                        $table7[$key7_3]['total'] += $row['general_public_budget'];
                        $table7[$key7_3]['fuli'] += $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['shangpin'] += $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['geren'] += $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['zhaiwu'] += $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['zibenjianshe'] += $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['ziben'] += $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['qiyejianshe'] += $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['qiye'] += $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['baozhang'] += $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0;
                        $table7[$key7_3]['qita'] += $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0;
                        // $table7[$key7_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                        // $table7[$key7_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
                    } else {
                        $table7[$key7_3] = [
                            'class' => $row['functional_category_code_class'],
                            'fund' => $row['functional_category_code_fund'],
                            'item' => $row['functional_category_code_item'],
                            'total' => $row['general_public_budget'],
                            // 'jbzc' => $row['project_category_name'] == "基本支出" ? $row['general_public_budget'] : 0,
                            // 'xmzc' => $row['project_category_name'] == "项目支出" ? $row['general_public_budget'] : 0,
                            'name' => $row['functional_category_name_item'],
                            'projectname' => $row['project_name'],
                            'fuli' => $row['economics_category_name_class'] == "工资福利支出" ? $row['general_public_budget'] : 0,
                            'shangpin' => $row['economics_category_name_class'] == "商品和服务支出" ? $row['general_public_budget'] : 0,
                            'geren' => $row['economics_category_name_class'] == "对个人和家庭的补助" ? $row['general_public_budget'] : 0,
                            'zhaiwu' => $row['economics_category_name_class'] == "债务利息及费用支出" ? $row['general_public_budget'] : 0,
                            'zibenjianshe' => $row['economics_category_name_class'] == "资本性支出（基本建设）" ? $row['general_public_budget'] : 0,
                            'ziben' => $row['economics_category_name_class'] == "资本性支出" ? $row['general_public_budget'] : 0,
                            'qiyejianshe' => $row['economics_category_name_class'] == "对企业补助（基本建设）" ? $row['general_public_budget'] : 0,
                            'qiye' => $row['economics_category_name_class'] == "对企业补助" ? $row['general_public_budget'] : 0,
                            'baozhang' => $row['economics_category_name_class'] == "对社会保障基金补助" ? $row['general_public_budget'] : 0,
                            'qita' => $row['economics_category_name_class'] == "其他支出" ? $row['general_public_budget'] : 0,
                        ];
                    }
                }
                // 收入预算 - 按一般公共预算汇总
                if (isset($zcbudget[$row['functional_category_code_class']])) {
                    $zcbudget[$row['functional_category_code_class']]['total'] += $row['general_public_budget'];
                } else {
                    $zcbudget[$row['functional_category_code_class']] = [
                        'total' => $row['general_public_budget'],
                        'title' => $row['functional_category_name_class'],
                        'use1' => $row['functional_category_name_fund'],
                        'use2' => $row['functional_category_name_item'],
                        'vmodel' => 'by' . $key,
                    ];
                }
            }
            // table 3 - level 1
            $key_1 = $row['functional_category_code_class'];
            $key_2 = $key_1 . $row['functional_category_code_fund'];
            $key_3 = $key_2 . $row['functional_category_code_item'];
            // table 3 - level 1
            if (isset($table3[$key_1 . '0000'])) {
                $table3[$key_1 . '0000']['total'] += $row['total'];
                $table3[$key_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                $table3[$key_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
            } else {
                $table3[$key_1 . '0000'] = [
                    'class' => $row['functional_category_code_class'],
                    'fund' => '', //$row['functional_category_code_fund'],
                    'item' => '', //$row['functional_category_code_item'],
                    'total' => $row['total'],
                    'jbzc' => $row['project_category_name'] == "基本支出" ? $row['total'] : 0,
                    'xmzc' => $row['project_category_name'] == "项目支出" ? $row['total'] : 0,
                    'name' => $row['functional_category_name_class'],
                ];
            }
            // table 3 - level 2
            if (isset($table3[$key_2 . '00'])) {
                $table3[$key_2 . '00']['total'] += $row['total'];
                $table3[$key_2 . '00']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                $table3[$key_2 . '00']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
            } else {
                $table3[$key_2 . '00'] = [
                    'class' => $row['functional_category_code_class'],
                    'fund' => $row['functional_category_code_fund'],
                    'item' => '', //$row['functional_category_code_item'],
                    'total' => $row['total'],
                    'jbzc' => $row['project_category_name'] == "基本支出" ? $row['total'] : 0,
                    'xmzc' => $row['project_category_name'] == "项目支出" ? $row['total'] : 0,
                    'name' => $row['functional_category_name_fund'],
                ];
            }
            // table 3 - level 3
            if (isset($table3[$key_3])) {
                $table3[$key_3]['total'] += $row['total'];
                $table3[$key_3]['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['total'] : 0;
                $table3[$key_3]['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['total'] : 0;
            } else {
                $table3[$key_3] = [
                    'class' => $row['functional_category_code_class'],
                    'fund' => $row['functional_category_code_fund'],
                    'item' => $row['functional_category_code_item'],
                    'total' => $row['total'],
                    'jbzc' => $row['project_category_name'] == "基本支出" ? $row['total'] : 0,
                    'xmzc' => $row['project_category_name'] == "项目支出" ? $row['total'] : 0,
                    'name' => $row['functional_category_name_item'],
                ];
            }

            // 收入预算 - 按total汇总
            if (isset($zcbudget2[$row['functional_category_code_class']])) {
                $zcbudget2[$row['functional_category_code_class']]['total'] += $row['total'];
            } else {
                $zcbudget2[$row['functional_category_code_class']] = [
                    'total' => $row['total'],
                    'title' => $row['functional_category_name_class'],
                    'use1' => $row['functional_category_name_fund'],
                    'use2' => $row['functional_category_name_item'],
                    'vmodel' => 'by' . $key,
                ];
            }

            // 政府性基金
            if ($row['government_budgets'] > 0) {
                $zf_jbzc += $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0;
                $zf_xmzc += $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0;
                if (isset($zfxjj[$key])) {
                    $zfxjj[$key]['total'] += $row['government_budgets'];
                    $zfxjj[$key]['c1'] += $row['government_budgets'];
                } else {
                    $zfxjj[$key] = [
                        'title' => $title,
                        'total' => $row['government_budgets'],
                        'c1' => $row['government_budgets'],
                        'c2' => 0,
                        'vmodel' => 'zfreason' . $key,
                        'c5' => 100
                    ];
                }

                // table 9 
                $key9_1 = $row['functional_category_code_class'];
                $key9_2 = $key9_1 . $row['functional_category_code_fund'];
                $key9_3 = $key9_2 . $row['functional_category_code_item'];
                // table 9 - level 1
                if (isset($table9[$key9_1 . '0000'])) {
                    $table9[$key9_1 . '0000']['total'] += $row['government_budgets'];
                    $table9[$key9_1 . '0000']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0;
                    $table9[$key9_1 . '0000']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0;
                } else {
                    $table9[$key9_1 . '0000'] = [
                        'class' => $row['functional_category_code_class'],
                        'fund' => '', //$row['functional_category_code_fund'],
                        'item' => '', //$row['functional_category_code_item'],
                        'total' => $row['government_budgets'],
                        'jbzc' => $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0,
                        'xmzc' => $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0,
                        'name' => $row['functional_category_name_class'],
                    ];
                }
                // table 9 - level 2
                if (isset($table9[$key9_2 . '00'])) {
                    $table9[$key9_2 . '00']['total'] += $row['government_budgets'];
                    $table9[$key9_2 . '00']['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0;
                    $table9[$key9_2 . '00']['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0;
                } else {
                    $table9[$key9_2 . '00'] = [
                        'class' => $row['functional_category_code_class'],
                        'fund' => $row['functional_category_code_fund'],
                        'item' => '', //$row['functional_category_code_item'],
                        'total' => $row['government_budgets'],
                        'jbzc' => $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0,
                        'xmzc' => $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0,
                        'name' => $row['functional_category_name_fund'],
                    ];
                }
                // table 9 - level 3
                if (isset($table9[$key9_3])) {
                    $table9[$key9_3]['total'] += $row['government_budgets'];
                    $table9[$key9_3]['jbzc'] += $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0;
                    $table9[$key9_3]['xmzc'] += $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0;
                } else {
                    $table9[$key9_3] = [
                        'class' => $row['functional_category_code_class'],
                        'fund' => $row['functional_category_code_fund'],
                        'item' => $row['functional_category_code_item'],
                        'total' => $row['government_budgets'],
                        'jbzc' => $row['project_category_name'] == "基本支出" ? $row['government_budgets'] : 0,
                        'xmzc' => $row['project_category_name'] == "项目支出" ? $row['government_budgets'] : 0,
                        'name' => $row['functional_category_name_item'],
                    ];
                }
            }
            // table 4
            if (isset($table4[$row['functional_category_code_class']])) {
                $table4[$row['functional_category_code_class']]['yiban'] += $row['general_public_budget'];
                $table4[$row['functional_category_code_class']]['zhengfu'] += $row['government_budgets'];
                $table4[$row['functional_category_code_class']]['guoyou'] += $row['government_capital_management_budget'];
                $table4[$row['functional_category_code_class']]['total'] += $row['general_public_budget'] + $row['government_budgets'] + $row['government_capital_management_budget'];
                // $table4[$row['functional_category_code_class']]['total'] += $row['total'];
            }
        }
        // 一般公共预算 去年
        $arr2 = [];
        // 政府性基金
        $zfxjj2 = [];
        foreach ($lastyears as $row) {
            $key = $row['functional_category_code_class'] . $row['functional_category_code_fund'] . $row['functional_category_code_item'];
            $title = $row['functional_category_name_class'] . '（类）' . $row['functional_category_name_fund'] . '（款）' . $row['functional_category_name_item'] . '（项）';

            if ($row['general_public_budget'] > 0) {
                if (isset($arr2[$key])) {
                    $arr2[$key]['total'] += $row['general_public_budget'];
                    // $arr2[$key]['c1'] += $row['general_public_budget'];
                } else {
                    $arr2[$key] = [
                        'title' => $title,
                        'total' => $row['general_public_budget'],
                        'c1' => 0,
                        'c2' => 0,
                        // 'vmodel' => 'reason20'
                    ];
                }
            }
            // 政府性基金
            if ($row['government_budgets'] > 0) {
                if (isset($zfxjj2[$key])) {
                    $zfxjj2[$key]['total'] += $row['government_budgets'];
                } else {
                    $zfxjj2[$key] = [
                        'title' => $title,
                        'total' => $row['government_budgets'],
                        'c1' => 0,
                        'c2' => 0,
                        // 'vmodel' => 'reason20'
                    ];
                }
            }
        }

        foreach ($arr as $key => $v) {
            if (isset($arr2[$key])) {
                $arr[$key]['c1'] = $arr[$key]['total'] - $arr2[$key]['total'];
                if ($arr2[$key]['total'] != 0) {
                    $arr[$key]['c5'] = $this->sabs(number_format(($arr[$key]['c1'] / $arr2[$key]['total']) * 100, 2, ".", ""));
                }
            }
            $arr[$key]['c3'] = $arr[$key]['c1'] < 0 ? '减少' : '增加';
            $arr[$key]['c4'] = $arr[$key]['c1'] < 0 ? '下降' : '增长';
            $arr[$key]['c1'] = $this->sabs(number_format($arr[$key]['c1'] / 10000, 2, ".", ""));
            $arr[$key]['total'] = number_format($arr[$key]['total'] / 10000, 2, ".", "");
            $arr[$key]['ltotal'] = isset($arr2[$key]) ? number_format($arr2[$key]['total'] / 10000, 2, ".", "") : 0;
        }

        // 政府性基金
        foreach ($zfxjj as $key => $v) {
            if (isset($zfxjj2[$key])) {
                $zfxjj[$key]['c1'] = $zfxjj[$key]['total'] - $zfxjj2[$key]['total'];
                if ($zfxjj2[$key]['total'] != 0) {
                    $zfxjj[$key]['c5'] = number_format(($zfxjj[$key]['c1'] / $zfxjj2[$key]['total']) * 100, 2, ".", "");
                }
            }
            $zfxjj[$key]['c1'] = number_format($zfxjj[$key]['c1'] / 10000, 2, ".", "");
            $zfxjj[$key]['total'] = number_format($zfxjj[$key]['total'] / 10000, 2, ".", "");
            $zfxjj[$key]['c3'] = $zfxjj[$key]['c1'] < 0 ? '减少' : '增加';
            $zfxjj[$key]['c4'] = $zfxjj[$key]['c1'] < 0 ? '下降' : '增长';
        }

        // 支出预算
        foreach ($zcbudget as $key => $v) {
            $zcbudget[$key]['rate'] = number_format(100 * ($zcbudget[$key]['total'] / $total), 2, ".", "");
            $zcbudget[$key]['total'] = number_format($zcbudget[$key]['total'] / 10000, 2, ".", "");
        }
        // 支出预算
        foreach ($zcbudget2 as $key => $v) {
            // $zcbudget[$key]['rate'] = number_format(100 * ($zcbudget[$key]['total'] / $total), 2, ".", "");
            $zcbudget2[$key]['total'] = $this->transformUnit($zcbudget2[$key]['total']);
        }
        // table 3
        foreach ($table3 as $k => $v) {
            $table3[$k]['total'] = $this->transformUnit($v['total']);
            $table3[$k]['jbzc'] = $this->transformUnit($v['jbzc']);
            $table3[$k]['xmzc'] = $this->transformUnit($v['xmzc']);
        }
        // table 9
        foreach ($table9 as $k => $v) {
            $table9[$k]['total'] = $this->transformUnit($v['total']);
            $table9[$k]['jbzc'] = $this->transformUnit($v['jbzc']);
            $table9[$k]['xmzc'] = $this->transformUnit($v['xmzc']);
        }
        // table 7
        foreach ($table7 as $k => $v) {
            $table7[$k]['total'] = $this->transformUnit($v['total']);
            $table7[$k]['fuli'] = $this->transformUnit($v['fuli']);
            $table7[$k]['shangpin'] = $this->transformUnit($v['shangpin']);
            $table7[$k]['geren'] = $this->transformUnit($v['geren']);
            $table7[$k]['zhaiwu'] = $this->transformUnit($v['zhaiwu']);
            $table7[$k]['zibenjianshe'] = $this->transformUnit($v['zibenjianshe']);
            $table7[$k]['ziben'] = $this->transformUnit($v['ziben']);
            $table7[$k]['qiyejianshe'] = $this->transformUnit($v['qiyejianshe']);
            $table7[$k]['qiye'] = $this->transformUnit($v['qiye']);
            $table7[$k]['baozhang'] = $this->transformUnit($v['baozhang']);
            $table7[$k]['qita'] = $this->transformUnit($v['qita']);
        }
        // table 4
        $i = 0;
        foreach ($table4 as $k => $v) {
            $table4[$k]['total'] = $this->transformUnit($v['total']);
            $table4[$k]['yiban'] = $this->transformUnit($v['yiban']);
            $table4[$k]['guoyou'] = $this->transformUnit($v['guoyou']);
            $table4[$k]['zhengfu'] = $this->transformUnit($v['zhengfu']);
            if ($i == 0) {
                $table4[$k]['xm'] = '财政拨款（补助）';
                $table4[$k]['hj'] = $this->transformUnit($fiscal_appropriation);
            }
            if ($i == 1) {
                $table4[$k]['xm'] = '一般公共预算';
                $table4[$k]['hj'] = $this->transformUnit($general_public_budget);
            }
            if ($i == 2) {
                $table4[$k]['xm'] = '政府性基金预算';
                $table4[$k]['hj'] = $this->transformUnit($government_budgets);
            }
            if ($i == 3) {
                $table4[$k]['xm'] = '国有资本经营预算';
                $table4[$k]['hj'] = $this->transformUnit($gov_capital);
            }
            $i++;
        }
        ksort($ryarrsort, 1);
        $_s1 = '';
        foreach ($ryarrsort as $k => $v) {
            $_s1 .= $v . "、";
        }
        ksort($gyarrsort, 1);
        $_s2 = '';
        foreach ($gyarrsort as $k => $v) {
            $_s2 .= $v . "、";
        }
        ksort($zcbudget, 1);
        ksort($table3, 1);
        ksort($table9, 1);
        ksort($table7, 1);
        foreach ($project_list as $k => $v) {
            $project_list[$k]['budget'] = $this->transformUnit($v['budget']) . "万元";
        }
        return $this->buildSuccess([
            "year" => date("Y"),
            "month" => date("m"),
            "day" => date("d"),
            "lists" => $arr,
            "projectlists" => $project_list,
            "gov" => $zfxjj,
            "zcbudget" => $zcbudget,
            "zcbudget2" => $zcbudget2,
            "zdata" => [
                "ryzc" => $ryzc / 10000,
                "gyzc" => $gyzc / 10000,
                // "ryarr" => implode(",", $ryarr),
                "ryarr" => trim($_s1, "、"),
                "gyarr" => trim($_s2, "、"),
                // "gyarr" => implode(",", $gyarr)
                'zf_jbzc' => $this->transformUnit($zf_jbzc),
                'zf_xmzc' => $this->transformUnit($zf_xmzc),
                'total7' => $this->transformUnit($total7),
                'table71' => $this->transformUnit($table71),
                'table72' => $this->transformUnit($table72),
                'table73' => $this->transformUnit($table73),
                'table74' => $this->transformUnit($table74),
                'table75' => $this->transformUnit($table75),
                'table76' => $this->transformUnit($table76),
                'table77' => $this->transformUnit($table77),
                'table78' => $this->transformUnit($table78),
                'table79' => $this->transformUnit($table79),
                'table710' => $this->transformUnit($table710),
                'projectcount' => count($project_list)
            ],
            'table3' => $table3,
            'table9' => $table9,
            'table7' => $table7,
            'table4' => $table4,
            // 'arr2' => $arr2,
            // 'arr1' => $arr
            // 'rrr' => $ryarrsort

        ]);
    }

    // 计算增长金额
    public function calc1($a, $b)
    {
        return $this->transformUnit($a - $b);
    }

    // 计算百分比
    public function calc2($a, $b)
    {
        if ($a == 0 && $b == 0) {
            return 0;
        }
        if ($b == 0) {
            return 100;
        }
        return 100 * (($a - $b) / $b);
    }
    // 计算total占比
    public function calc3($a, $b)
    {
        if ($b == 0) {
            return 100;
        }
        return 100 * ($a / $b);
    }
    public function transformUnit($a)
    {
        // return $a;
        return number_format($a / 10000, 2, ".", "");
    }
    public function formatunit($a)
    {
        return number_format($a, 2, ".", "");
    }
    public function initTable4()
    {
        $arr = [];
        $lists = Db::query("select DISTINCT(functional_category_code_class) as functional_category_code_class,functional_category_name_class from admin_budget");
        foreach ($lists as $row) {
            $arr[$row['functional_category_code_class']] = [
                'name' => $row['functional_category_name_class'],
                'code' => $row['functional_category_code_class'],
                'total' => 0,
                'yiban' => 0,
                'zhengfu' => 0,
                'guoyou' => 0,
                'xm' => '',
                'hj' => 0,
            ];
        }
        ksort($arr, 1);
        // $arr[0]['xm'] = 'xxxx';
        return $arr;
    }
    public function sabs($a)
    {
        return str_replace("-","",$a);
    }
}
